﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FacebookWrapper.ObjectModel;

namespace Desktop_Facebook
{
	internal class CSVSerializer
	{
		private const string k_Seperator = ",";
		private const string k_BirthdaySubjectLine = @"{0}'s Birthday!";

		public void Serialize(FileStream i_SaveFile, List<Message> i_MessagesToSave)
		{
			appendTextToFile(i_SaveFile, "id" + k_Seperator);
			appendTextToFile(i_SaveFile, "time" + k_Seperator);
			appendTextToFile(i_SaveFile, "from" + k_Seperator);
			appendTextToFile(i_SaveFile, "text" + Environment.NewLine);

			foreach (Message currentMessage in i_MessagesToSave)
			{
				appendTextToFile(i_SaveFile, currentMessage.Id.ToString() + k_Seperator);
				appendTextToFile(i_SaveFile, currentMessage.CreatedTime.Value.ToString() + k_Seperator);
				appendTextToFile(i_SaveFile, currentMessage.From.Name.ToString() + k_Seperator);
				appendTextToFile(i_SaveFile, fixString(currentMessage.Text.ToString()) + Environment.NewLine);
			}
		}

		public void Serialize(FileStream i_SaveFile, Dictionary<string, CustomDate> i_BirthdaysToSave)
		{
			if (Session.Instance.LoggedInUser != null)
			{
				appendTextToFile(i_SaveFile, "Subject" + k_Seperator);
				appendTextToFile(i_SaveFile, "Start Date" + k_Seperator);
				appendTextToFile(i_SaveFile, "End Date" + k_Seperator);
				appendTextToFile(i_SaveFile, "All Day Event" + k_Seperator);
				appendTextToFile(i_SaveFile, "Description" + Environment.NewLine);

				foreach (User currentFriend in Session.Instance.LoggedInUser.Friends)
				{
					if (i_BirthdaysToSave.ContainsKey(currentFriend.Id))
					{
						CustomDate currentBirtday = i_BirthdaysToSave[currentFriend.Id];
						string dateString = string.Format(@"{0}/{1}/{2}", currentBirtday.Date.Day.ToString(), currentBirtday.Date.Month.ToString(), DateTime.Today.Year.ToString());

						appendTextToFile(i_SaveFile, string.Format(k_BirthdaySubjectLine, currentFriend.Name.ToString()) + k_Seperator);
						appendTextToFile(i_SaveFile, dateString + k_Seperator);
						appendTextToFile(i_SaveFile, dateString + k_Seperator);
						appendTextToFile(i_SaveFile, Boolean.TrueString + k_Seperator);
						appendTextToFile(i_SaveFile, string.Format(k_BirthdaySubjectLine, currentFriend.Name.ToString()) + Environment.NewLine);
					}
				}
			}
		}

		public void Serialize(FileStream i_SaveFile, List<Event> i_EventsToSave)
		{
			appendTextToFile(i_SaveFile, "Name" + k_Seperator);
			appendTextToFile(i_SaveFile, "Start Date" + k_Seperator);
			appendTextToFile(i_SaveFile, "Start Time" + k_Seperator);
			appendTextToFile(i_SaveFile, "End Date" + k_Seperator);
            appendTextToFile(i_SaveFile, "End Time" + Environment.NewLine);

			foreach (Event currentEvent in i_EventsToSave)
			{
				appendTextToFile(i_SaveFile, currentEvent.Name.ToString());
				appendTextToFile(i_SaveFile, currentEvent.StartTime.Value.Date.ToShortDateString() + k_Seperator);
				appendTextToFile(i_SaveFile, currentEvent.StartTime.Value.ToShortTimeString() + k_Seperator);
				appendTextToFile(i_SaveFile, currentEvent.EndTime.Value.Date.ToShortDateString() + k_Seperator);
				appendTextToFile(i_SaveFile, currentEvent.EndTime.Value.ToShortTimeString() + Environment.NewLine);
			}
		}

		private static void appendTextToFile(FileStream i_FileStream, string i_Text)
		{
			Encoding hebrewEncoding = Encoding.GetEncoding("windows-1255");
			byte[] toWrite = hebrewEncoding.GetBytes(i_Text);
			
			i_FileStream.Write(toWrite, 0, toWrite.Length);
		}

		private string fixString(string i_String)
		{
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < i_String.Length; i++)
			{
				if (i_String[i] != k_Seperator[0] && i_String[i] != '\n')
				{
					builder.Append(i_String[i]);
				}
			}

			return builder.ToString();
		}
	}
}
